//
//  ST_CallCountDownAlertController.swift
//  btvideo
//
//  Created by mac on 2021/3/10.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit

class ST_CallCountDownAlertController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(root_view_width)
            make.height.equalTo(root_view_height)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
//            .offset(-ScreenHeight/10)
        }
        
        root_view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(cover_image_size)
            make.top.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(23)
            make.centerX.equalToSuperview()
            make.height.equalTo(22)
        }
        
        
        root_view.addSubview(count_down_label)
        count_down_label.snp.makeConstraints { (make) in
           
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.centerX.equalToSuperview()
            make.height.equalTo(48)
            
        }
        
        
        root_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
           
            make.top.equalTo(titleLabel.snp.bottom).offset(58)
            make.centerX.equalToSuperview()
            make.height.equalTo(20)
            
        }
        
        
        root_view.addSubview(cancel_btn)
        cancel_btn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-8)
            make.width.equalTo(200)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.bottom.equalTo(cancel_btn.snp.top)
            make.width.equalTo(200)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
        }
        
        count_down_loop()
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(count_down_loop), userInfo: nil, repeats: true)
        
    }
    
    
    deinit {
        
        timer?.invalidate()
        timer = nil
    }
    
    @objc fileprivate func count_down_loop() {
        
        let date = seconds_to_hours_minutes_seconds(seconds: count_down_seconds)
        
        let minutes = date.minute
        
        let seconds = date.second
        
        count_down_label.text = String(format: "%02d:%02d", minutes,seconds)
        
        count_down_seconds -= 1
        
        
        if count_down_seconds < 0{
            
            timer?.invalidate()
            
            timer = nil
        }
        
    }
    
//    MARK:- Properties
   @objc var root_view_height = 383.cgFloat
    
    @objc var root_view_width = 300.cgFloat
    
    var count_down_seconds = 30
    
    @objc var cover_image_size = CGSize.init(width: 202, height: 119)
    
    fileprivate var timer:Timer?
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.white
        control.cornerRadius = 20
        control.borderColor = UIColor.init(hexString: "#979797")
        control.borderWidth = 1
        return control
    }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("kValue不够了"))
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        let attrString = NSMutableAttributedString(string: "剩余甜心币还可通话")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        
        return control
    }()
    
    fileprivate lazy var count_down_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 34, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "加点甜心币跟小姐姐继续甜聊呀~")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.38, green: 0.4, blue: 0.44, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedText = attrString
        
        return control
    }()
    

    fileprivate lazy var confirm_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.init(hexString: "#FEC207")
        control.cornerRadius = 36/2.0
        control.setTitleForAllStates("获取更多")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    fileprivate lazy var cancel_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = UIColor.clear
        control.cornerRadius = 36/2.0
        control.setTitleForAllStates("稍后再说")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.regular)
        control.setTitleColorForAllStates(UIColor.init(hexString: "#61666F")!)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
//    MARK: - Public
    var dismiss_after_action = true
    @objc var confirm_closure:(()->())?
    @objc var cancel_closure:(()->())?
    
}


extension ST_CallCountDownAlertController{
    
    @objc func confirm_handle(){
        
        if dismiss_after_action{
            self.dismiss(animated: false) {
                self.confirm_closure?()
            }
        }else{
            self.confirm_closure?()
        }
    }
    
    @objc func cancel_handle(){
        
        self.dismiss(animated: false) {
            self.cancel_closure?()
        }
    }
    
    
}
